package com.cabrida.livestar;

import java.lang.Thread.UncaughtExceptionHandler;
import java.util.Timer;
import java.util.TimerTask;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Looper;
import android.widget.Toast;

/**
 */
public class BaseException implements UncaughtExceptionHandler {
    private static BaseException sInstance = new BaseException();
    private UncaughtExceptionHandler mHandler;
    private Context mContext;

    private BaseException() {

    }

    public static BaseException getInstance() {
        return sInstance;
    }

    public void init(Context context) {
        // 获取系统默认的UncaughtException处理
        mContext = context.getApplicationContext();
        mHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        // 如果用户没有处理则让系统默认的异常处理器来处理
        if (!handleException(ex) && mHandler != null) {
            mHandler.uncaughtException(thread, ex);
        }
    }

    /**
     * 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成
     * 
     * @param ex
     * @return true:是否已处理异常信息
     */
    private boolean handleException(Throwable ex) {
        if (ex == null) {
            return false;
        } else {
            Looper.prepare();
            Toast.makeText(mContext, "系统异常,应用重启中...", Toast.LENGTH_SHORT)
                    .show();
            Timer timer = new Timer();
            TimerTask task = new TimerTask() {

                @Override
                public void run() {
                    PackageManager pm = mContext.getPackageManager();
                    if (pm != null) {
                        Intent mStartActivity = pm
                                .getLaunchIntentForPackage(mContext
                                        .getPackageName());
                        if (mStartActivity != null) {
                            mStartActivity
                                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            int mPendingIntentId = 223344;
                            PendingIntent mPendingIntent = PendingIntent
                                    .getActivity(mContext, mPendingIntentId,
                                            mStartActivity,
                                            PendingIntent.FLAG_CANCEL_CURRENT);
                            AlarmManager mgr = (AlarmManager) mContext
                                    .getSystemService(Context.ALARM_SERVICE);
                            mgr.set(AlarmManager.RTC,
                                    System.currentTimeMillis() + 1000,
                                    mPendingIntent);
                            System.exit(0);
                        } else {
                            System.out
                                    .println("Cant restart app activity is null");
                        }
                    } else {
                        System.out.println("Cant restart app manager is null");
                    }
                }
            };
            timer.schedule(task, 1000);
            Looper.loop();
            return true;
        }
    }
}
